
#include <stdbool.h>
#include <aos/aos.h>
#include <yoc/yoc.h>
#include <yoc/partition.h>
#include <aos/kv.h>
#include <devices/devicelist.h>
#include <devices/uart.h>
#include <k_api.h>
#include <key_mgr.h>

#include "hci_hal_h4.h"
#include "pin_name.h"
#include "app_init.h"
#include "pin.h"
#include <vfs.h>
#include <vfs_cli.h>
#if defined (CONFIG_SAVE_JS_TO_RAM) && (CONFIG_SAVE_JS_TO_RAM == 1)
#else
#include <littlefs_vfs.h>
#endif

const char *TAG = "INIT";

#ifndef CONSOLE_UART_IDX
#define CONSOLE_UART_IDX 0
#endif

extern size_t __psram_data_start__;
extern size_t __psram_data_end__;
extern k_mm_head *g_kmm_head;
k_mm_region_t mm_region[] = {
    {(uint8_t *)&__psram_data_end__, (size_t)1024*6000},
};

void board_mm_init()
{
    memset((void *)&__psram_data_start__, 0, (uint32_t)&__psram_data_end__ - (uint32_t)&__psram_data_start__);
    krhino_add_mm_region(g_kmm_head, mm_region[0].start, mm_region[0].len);
}


void board_yoc_init()
{
    board_mm_init();
    board_base_init();

    console_init(CONSOLE_UART_IDX, 1000000, 128);

    ulog_init();
    aos_set_log_level(AOS_LL_DEBUG);

    LOGI(TAG, "Build:%s,%s",__DATE__, __TIME__);
    /* load partition */
    int ret = partition_init();
    if (ret <= 0) {
        LOGE(TAG, "partition init failed");
    } else {
        LOGI(TAG, "find %d partitions", ret);
    }

    aos_kv_init("kv");

    km_init();

    extern void board_bt_reset();
    board_bt_reset();

    uart_config_t uart_config;
    uart_config_default(&uart_config);

    uart_config.baud_rate = 1000000;

    extern void bt_hci_uart_h5_register(int uart_idx, uart_config_t config);
    bt_hci_uart_h5_register(CONFIG_HCI_UART_IDX, uart_config);

    extern int hci_h5_driver_init();
    hci_h5_driver_init();

    board_cli_init();
#if defined (CONFIG_SAVE_JS_TO_RAM) && (CONFIG_SAVE_JS_TO_RAM == 1)
#else
    vfs_init();
    ret = vfs_lfs_register("lfs");
    if (ret != 0) {
        LOGE(TAG, "littlefs register failed(%d)", ret);
    }
#endif
}
